package soexample.umeng.com.video;

import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import cn.jzvd.JZVideoPlayer;
import cn.jzvd.JZVideoPlayerStandard;
import soexample.umeng.com.video.guide.Guide;
import soexample.umeng.com.video.guide.GuideBuilder;
import soexample.umeng.com.video.guide.SimpleConponent;

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRvActivityMainCount;
    private BaseQuickAdapter  adapter;
    private List<String> strings=new ArrayList<String>();
    private JZVideoPlayerStandard videoPlayer;
    public int firstVisible=0,visibleCount=0, totalCount=0;
    private FrameLayout currentPlayVideo;
    private Guide guide;
//     "http://jzvd.nathen.cn/c494b340ff704015bb6682ffde3cd302/64929c369124497593205a4190d7d128-5287d2089db37e62345123a1be272f8b.mp4",
//             "http://jzvd.nathen.cn/63f3f73712544394be981d9e4f56b612/69c5767bb9e54156b5b60a1b6edeb3b5-5287d2089db37e62345123a1be272f8b.mp4",
//             "http://jzvd.nathen.cn/b201be3093814908bf987320361c5a73/2f6d913ea25941ffa78cc53a59025383-5287d2089db37e62345123a1be272f8b.mp4",
//             "http://jzvd.nathen.cn/d2438fd1c37c4618a704513ad38d68c5/68626a9d53ca421c896ac8010f172b68-5287d2089db37e62345123a1be272f8b.mp4",
//             "http://jzvd.nathen.cn/25a8d119cfa94b49a7a4117257d8ebd7/f733e65a22394abeab963908f3c336db-5287d2089db37e62345123a1be272f8b.mp4",
//             "http://jzvd.nathen.cn/7512edd1ad834d40bb5b978402274b1a/9691c7f2d7b74b5e811965350a0e5772-5287d2089db37e62345123a1be272f8b.mp4",
//             "http://jzvd.nathen.cn/c6e3dc12a1154626b3476d9bf3bd7266/6b56c5f0dc31428083757a45764763b0-5287d2089db37e62345123a1be272f8b.mp4"

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();



    }

    private void initView() {
        mRvActivityMainCount = (RecyclerView) findViewById(R.id.rv_activity_main_count);
        LinearLayoutManager manager=new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        mRvActivityMainCount.setLayoutManager(manager);
        strings.add("http://jzvd.nathen.cn/c494b340ff704015bb6682ffde3cd302/64929c369124497593205a4190d7d128-5287d2089db37e62345123a1be272f8b.mp4");
        strings.add("http://jzvd.nathen.cn/c494b340ff704015bb6682ffde3cd302/64929c369124497593205a4190d7d128-5287d2089db37e62345123a1be272f8b.mp4");
        strings.add("http://jzvd.nathen.cn/c494b340ff704015bb6682ffde3cd302/64929c369124497593205a4190d7d128-5287d2089db37e62345123a1be272f8b.mp4");
        strings.add("http://jzvd.nathen.cn/c494b340ff704015bb6682ffde3cd302/64929c369124497593205a4190d7d128-5287d2089db37e62345123a1be272f8b.mp4");
        strings.add("http://jzvd.nathen.cn/c494b340ff704015bb6682ffde3cd302/64929c369124497593205a4190d7d128-5287d2089db37e62345123a1be272f8b.mp4");
        strings.add("http://jzvd.nathen.cn/c494b340ff704015bb6682ffde3cd302/64929c369124497593205a4190d7d128-5287d2089db37e62345123a1be272f8b.mp4");
        strings.add("http://jzvd.nathen.cn/c494b340ff704015bb6682ffde3cd302/64929c369124497593205a4190d7d128-5287d2089db37e62345123a1be272f8b.mp4");
        strings.add("http://jzvd.nathen.cn/c494b340ff704015bb6682ffde3cd302/64929c369124497593205a4190d7d128-5287d2089db37e62345123a1be272f8b.mp4");
        strings.add("http://jzvd.nathen.cn/c494b340ff704015bb6682ffde3cd302/64929c369124497593205a4190d7d128-5287d2089db37e62345123a1be272f8b.mp4");
        strings.add("http://jzvd.nathen.cn/c494b340ff704015bb6682ffde3cd302/64929c369124497593205a4190d7d128-5287d2089db37e62345123a1be272f8b.mp4");


        adapter=new BaseQuickAdapter<String,BaseViewHolder>(R.layout.item_video,strings) {

            @Override
            protected void convert(BaseViewHolder helper, String item) {
              videoPlayer=helper.getView(R.id.item_video_player);
              videoPlayer.setUp(item,JZVideoPlayer.SCREEN_WINDOW_LIST,"");

                Glide.with(videoPlayer.getContext())
                        .load(VideoConstant.videoThumbs[0][helper.getLayoutPosition()])
                        .into(videoPlayer.thumbImageView);

            }
        };
        mRvActivityMainCount.setAdapter(adapter);
        mRvActivityMainCount.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                switch (newState){
                    case RecyclerView.SCROLL_STATE_DRAGGING:
                        if(null!=guide){
                            guide.dismiss();
                        }
                        //当手指接触屏幕开始滑动的时候
                        Log.e("TGA","SCROLL_STATE_DRAGGING");
                        break;
                    case RecyclerView.SCROLL_STATE_IDLE:
                        //当滑动已经停止,闲置
                        autoPlayVideo(recyclerView);
                        Log.e("TGA","SCROLL_STATE_IDLE");
                        break;
                    case RecyclerView.SCROLL_STATE_SETTLING:
                        Log.e("TGA","SCROLL_STATE_SETTLING");
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                Log.e("TGA","onScrolled");
                RecyclerView.LayoutManager layoutManager=recyclerView.getLayoutManager();
                if(layoutManager instanceof LinearLayoutManager){
                    firstVisible=((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
                    int last=((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
                    visibleCount=last-firstVisible;
                    totalCount=strings.size();

                }

            }

        });

    }

    public int getCurrentViewIndex(LinearLayoutManager mLineManager) {
        int firstVisibleItem = mLineManager.findFirstVisibleItemPosition();
        int lastVisibleItem = mLineManager.findLastVisibleItemPosition();
        int currentIndex = firstVisibleItem;
        int lastHeight = 0;
        for (int i = firstVisibleItem; i <= lastVisibleItem; i++) {
            View view = mLineManager.getChildAt(i - firstVisibleItem);
            if (null == view) {
                continue;
            }

            int[] location = new int[2];
            view.getLocationOnScreen(location);

            Rect localRect = new Rect();
            view.getLocalVisibleRect(localRect);

            int showHeight = localRect.bottom - localRect.top;
            if (showHeight > lastHeight) {
                currentIndex = i;
                lastHeight = showHeight;
            }
        }

        if (currentIndex < 0) {
            currentIndex = 0;
        }

        return currentIndex;
    }

    private void autoPlayVideo(RecyclerView view){
        Log.e("videoTest", "firstVisiblePos = " + firstVisible + "visibleItemCount = " + visibleCount);
        for (int i = 0; i < visibleCount; i++) {
            if (view!=null&&view.getChildAt(i)!=null&&view.getChildAt(i).findViewById(R.id.item_video_player) != null) {
                currentPlayVideo=(FrameLayout) view.getChildAt(i).findViewById(R.id.fl_content);
                JZVideoPlayerStandard videoPlayerStandard1 = (JZVideoPlayerStandard) view.getChildAt(i).findViewById(R.id.item_video_player);
                Rect rect = new Rect();
                videoPlayerStandard1.getLocalVisibleRect(rect);
                int videoheight3 = videoPlayerStandard1.getHeight();
                Log.e("videoTest","i="+i+"==="+"videoheight3:"+videoheight3+"==="+"rect.top:"+rect.top+"==="+"rect.bottom:"+rect.bottom);
                if (rect.top==0&&rect.bottom==videoheight3) {
                    if (videoPlayerStandard1.currentState == JZVideoPlayer.CURRENT_STATE_NORMAL || videoPlayerStandard1.currentState == JZVideoPlayer.CURRENT_STATE_ERROR) {
                        Log.e("videoTest", videoPlayerStandard1.currentState + "======================performClick======================");
                        videoPlayerStandard1.startButton.performClick();
                        currentPlayVideo.setBackgroundColor(Color.parseColor("#ffffff"));
                        //setCurrentMasking(currentPlayVideo);
                    }
                    return;
                }

            }
        }
        Log.e("videoTest", "======================releaseAllVideos=====================");
        JZVideoPlayer.releaseAllVideos();
    }

    private void setCurrentMasking(LinearLayout currentPlayVideo) {
        GuideBuilder threeBuilder = new GuideBuilder();
        threeBuilder.setTargetView(currentPlayVideo)
                .setAlpha(204)
                .setHighTargetCorner(0)
                .setHighTargetPadding(0)
                .setOverlayTarget(false)
                .setOutsideTouchable(false);
        threeBuilder.setOnVisibilityChangedListener(new GuideBuilder.OnVisibilityChangedListener() {
            @Override
            public void onShown() {
            }

            @Override
            public void onDismiss() {

            }
        });
        threeBuilder.addComponent(new SimpleConponent());
        guide = threeBuilder.createGuide();
        guide.setShouldCheckLocInWindow(false);
        guide.show(this);
    }

    @Override
    public void onBackPressed() {
        if (JZVideoPlayer.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        JZVideoPlayer.releaseAllVideos();
    }
}
